package com.app.wechart.Controllers;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.wechart.ChartManager.MessageInfo;
import com.app.wechart.ChartManager.MessageListAdapter;
import com.app.wechart.ChartManager.MessageModule;
import com.app.wechart.R;
import com.app.wechart.SocketUntitle.JWebSocketClient;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class RootViewActivity extends AppCompatActivity implements TextView.OnEditorActionListener, View.OnClickListener {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.root_activity);
        MessageModule.getInstance().RegistWebSocket("1");
        this.loadList();
        this.addKeyBoardMonitor();
    }


    private void loadList() {
        ArrayList list = new ArrayList();
        RecyclerView recyclerView = findViewById(R.id.chart_msg_list);
        MessageListAdapter adapter = new MessageListAdapter(this, list);
        @SuppressLint("WrongConstant") LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayout.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        MessageModule.getInstance().loderMessage(this, adapter,recyclerView);
    }

    private void addKeyBoardMonitor() {
        EditText editText = findViewById(R.id.input_message);
        editText.setOnEditorActionListener(this);

        Button btn = findViewById(R.id.send_msg);
        btn.setOnClickListener(this);
    }


    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_SEND) {
            this.sendMsg(String.valueOf(v.getText()));
            JSONObject jsonObject = new JSONObject();
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        EditText editText = findViewById(R.id.input_message);
        this.sendMsg(String.valueOf(editText.getText()));
    }

    private void sendMsg(String msg){
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.userId = "1";
        messageInfo.msg = msg;
        messageInfo.toUserId = "2";
        messageInfo.userName = "pigone";
        messageInfo.fromType = "1";
        MessageModule.getInstance().SendMsg(messageInfo);
    }
}
